<?php


namespace app\common\model\xilujob;


use think\Model;
use function fast\array_get;

class ResumeSchameRelation extends Model
{
    protected $name = 'xilujob_resume_schame_relation';


    /**
     * @param $resume
     * @param $data
     * @return mixed
     * @throws \Exception
     */
    public function add($resume) {
        $schema = ResumeSchema::get_schema();
        $schema_ids = array_column($schema,'id');
        try {
            if(!$schema_ids) {
                self::where('resume_id', $resume->id)->delete();
            }else {
                $existed_schema_ids = self::where('resume_id', $resume->id)
                    ->where('schema_id','in',$schema_ids)
                    ->column('schema_id');

                $insert_ids = array_diff($schema_ids, $existed_schema_ids);
                foreach ($insert_ids as $insert_id) {
                    self::create([
                        'resume_id'=>$resume->id,
                        'schema_id'=>$insert_id
                    ]);
                }
                self::where('resume_id', $resume->id)
                    ->where('schema_id', 'not in', $schema_ids)
                    ->delete();
            }
            return self::where('schema_id', 'in', $schema_ids)->select();
        }catch (\Exception $e) {
            throw $e;
        }
    }

}